package demo.practice.leetcode_base;

public class Offer24 {

    public static void main(String[] args) {
        String repeat = "Java".repeat(2);

        ListNode h = new ListNode(4);
        h.next = new ListNode(5);
        h.next.next = new ListNode(1);
        h.next.next.next = new ListNode(9);

        Offer24 offer24 = new Offer24();
        offer24.reverseList(h);
    }

    public ListNode reverseList(ListNode head) {

        if (head==null||head.next == null)
            return head;
        //反转链表
        ListNode current = head;
        ListNode parent = new ListNode(current.next.val);
        parent.next = new ListNode(current.val);
        ListNode pre;
        while (current.next != null) {
            current = current.next;
            if (current.next != null) {
                pre = parent;
                parent = new ListNode(current.next.val);
                parent.next = pre;
            }
        }
        return parent;
    }
}
